import React, { Component } from 'react';
import { HashRouter as Router, withRouter } from 'react-router-dom';
// 这个工具用来将定义好的路由对象转换为路由结构
import { renderRoutes } from 'react-router-config';

import Index from 'pages/Index';
import Login from 'pages/Login';
import SearchPromotionPage from 'pages/SearchPromotion';
import App from '../App';

const routes = [
    {
        path: '/index',
        component: Index,
        exact: false, // 是否精确匹配
        title: '',
    },
    {
        path: '/searchPromotion',
        exact: false,
        component: SearchPromotionPage,
        title: '',
    },
    {
        path: '/login',
        component: Login,
        exact: false, // 是否精确匹配
        title: '',
    },
];

// 使用withRouter高阶组件处理App,使app组件变为一个路由组件
const AppWrapComp = withRouter(App);

class AppRoute extends Component {
    render() {
        return (
            <div>
                <Router>
                    <AppWrapComp>
                        {renderRoutes(routes.map((item) => ({ ...item, key: item.path })))}
                    </AppWrapComp>
                </Router>
            </div>
        );
    }
}

export default AppRoute;
